﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Panorama.Web.CustomAttributes
{
    public class UserNameValidator : ValidationAttribute
    {
        private readonly int userNameMaxLength = 256;

        public UserNameValidator()
        {
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                var userName = value.ToString();
                if (string.IsNullOrWhiteSpace(userName) || userName.Length > userNameMaxLength || userName.Contains(@"\"))
                {
                    var errorMessage = FormatErrorMessage(validationContext.DisplayName);
                    return new ValidationResult(errorMessage);
                }
                
            }

            return ValidationResult.Success;  
        }
    }
}